package cn.t.util.common.jmx;

import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;

public class JmxUtil {

    public static MBeanServer createMBeanServer(String domain) {
        return MBeanServerFactory.createMBeanServer(domain);
    }

    public static MBeanServer createMBeanServer() {
        return MBeanServerFactory.createMBeanServer();
    }

    public static ObjectName createObjectName(String domain, String attributes) throws MalformedObjectNameException {
        return new ObjectName(generateKey(domain, attributes));
    }

    private static String generateKey(String domain, String attributes) {
        if (domain == null && attributes == null) {
            return "";
        } else if (domain == null) {
            return attributes;
        } else if (attributes == null) {
            return domain;
        } else {
            return domain + ":" + attributes;
        }
    }


}
